home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The 640 MEG Shareware Studio 2
/
The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO
/
pascal
/
sort_stm.zip
/
TEST.PAS
< prev
Wrap
Pascal/Delphi Source File
|
1992-10-18
|
5KB
|
187 lines
Program Test;
{a complete Testing and development program for demonstrating the indexed }
{Stream Methods/objects}
uses DOS,Objects,SIndexed,crt;
Type
pTestObj = ^Testobj;
TestObj = OBJECT(IndexObj)
company: Pstring;
amountOwed: Real;
Constructor INIT(Comp: String; Amount: Real);
Destructor Done; VIRTUAL;
Function COMPARE(t: pIndex): Integer; VIRTUAL;
Constructor Load(VAR s:tstream);
Procedure store(VAR s: tstream);
end;
var Index: pSortedIndex;
SearchObj: pTestObj;
const
RtestObj: TStreamRec = (
ObjType: 10000;
VmtLink: Ofs(TypeOf(TestObj)^);
Load: @testObj.Load;
Store: @TestObj.Store);
{this controls how many "dummy" records are created for our test purposes!}
{during creation, program can store about 300 or so records per MINUTE}
{when using a SORTEDIndexstream or about 1250 when using a indexstream}
{however the SEARCH routines on an indexstream are CONSIDERABLY slower then}
{a Sorted stream}
ITemMax: word = 1000;
CONSTRUCTOR TestObj.Init(Comp: String; amount: Real);
begin
IndexObj.Init;
Company:=NewStr(comp);
amountOwed:=Amount;
end;
DESTRUCTOR TestObj.Done;
begin
if Company<>NIL then
disposeStr(company);
tobject.done;
end;
CONSTRUCTOR TestObj.Load(VAR s: tStream);
begin
Company:=s.ReadStr;
s.Read(AmountOwed,Sizeof(AmountOwed));
end;
Procedure Testobj.Store(VAR s: tStream);
begin
s.WriteStr(Company);
s.Write(AmountOwed,sizeof(AmountOwed));
end;
FUNCTION testObj.Compare(T: pIndex): Integer;
VAR TEST: pTestObj Absolute T;
begin
if Test^.Company^ < Company^ then compare :=1 else
if Test^.Company^ = Company^ then Compare:=0 else
Compare:=-1;
{ writeln('SEARCHING: ',Test^.Company^:15,' Against ',Company^);
write('Press any key...');
readln;
writeln;}
end;
Procedure RegisterTest;
begin
RegisterType(rTestobj);
end;
Procedure StoreSomeData;
VAR I,N,X,Ourpos: Word;
s: String;
amt: Real;
temp: pTestObj;
begin
Temp:=New(pTestObj,Init('',0.00));
OurPos:=ItemMax - 2; {store a KNOWN record at this position!}
Randomize;
for I:=0 to ItemMax do
begin
if I=ourpos then
begin
S:='OUR COMPANY';
Amt:=1.48;
end
else
begin
s:='';
for N:=1 to 9 do
S:=S+CHR(RANDOM(24)+65);
X:=Random(10)+1;
for N:=1 to X do
S:=S+CHR(RANDOM(24)+65);
amt:=Random(1000)*1.4951;
end;
Writeln(I:4,' Creating ',s,Amt:10:2);
if Temp^.Company<>NIL then
disposeStr(temp^.Company);
Temp^.Company:=NewStr(S);
Temp^.Amountowed:=Amt;
Index^.Insert(Temp); {note call to INSERT and NOT PUT!}
end;
end;
Procedure SearchMe;
{search indexstream for objects test routine only!}
VAR searchobj: ptestObj;
s: String;
Searches,N,X: Byte;
begin
{Now FINALLY Set up and do our search!}
SearchObj:=NEW(pTestObj,Init('OUR COMPANY',0.00));
if SearchObj<>NIL then
begin
{search for our KNOWN name...}
write('Searching for: ',searchobj^.Company^,'...');
if Index^.Find(SearchObj) then
writeln('FOUND!') else Writeln('NOT FOUND!');
for Searches:=1 to 5 do {now search for 5 randomly generated names...}
begin
s:='';
for N:=1 to 9 do
S:=S+CHR(RANDOM(24)+65);
X:=Random(10)+1;
for N:=1 to X do
S:=S+CHR(RANDOM(24)+65);
{Replace company name...}
if searchobj^.Company<>NIL then
disposestr(Searchobj^.Company);
Searchobj^.Company:=NewStr(S);
write('Searching for: ',searchobj^.Company^,'...');
if Index^.Find(SearchObj) then
writeln('FOUND!') else Writeln('NOT FOUND!');
end;
end;
writeln('Stream contains ',Index^.ItemCount,' Items');
end;
{Simply displays entire contents of stream...}
Procedure PrintMe;
VAR Tmp: pTestObj;
begin
Index^.ItemPos(0);
while Index^.GetPos<Index^.GetSize do
begin
Tmp:=pTestObj(Index^.get);
writeln(Tmp^.Company^,Tmp^.AmountOwed:10:2);
dispose(tmp,done);
end;
end;
begin
RegisterTest;
Index:=NEW(pSortedIndex,INIT(64*1024,'TEST.DAT'));
if Index^.ItemCount=0 then
storesomedata;
searchMe;
(* Index^.ReBuild; {test redindex procedure}
writeln;
Searchme; {verify Rebuild} *)
{ PrintMe; } {uncomment if you wish to see sorted list printed}
Writeln;
dispose(Index,done);
end.